package com.szqhyxj.youxiangju.biz.partnerHome.shop.order;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.sys.components.LoadImageUtils;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.view.popview.MsgChoicePopView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindDrawable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 确定发货设备列表
 */
public class DeliveryOrderActivity extends BaseActivity implements DeliveryOrderContract.View, NavigationBar.NavigationBarListener, MsgChoicePopView.OnClickListener{

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.order_list)
    ListView orderList;

    @BindDrawable(R.drawable.background__yuan)
    Drawable yuan;
    @BindDrawable(R.mipmap.xuanzhong_15)
    Drawable xuanzhong;

    private static final String ORDERID = "orderId";
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.submit_container)
    LinearLayout submitContainer;

    private DeliveryOrderAdapter adapter;
    private List<StockPOListItem> listItems = new ArrayList<>();
    private DeliveryOrderPresenter presenter;
    private HeaderViewHolder headerViewHolder;
    private int number;

    public static void start(Activity activity, String orderId){
        Intent intent = new Intent();
        intent.setClass(activity, DeliveryOrderActivity.class);
        intent.putExtra(ORDERID, orderId);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delivery_order);
        ButterKnife.bind(this);
        initView();
        navigationBar.setNavigationBarListener(this);
        adapter = new DeliveryOrderAdapter(listItems);
        adapter.setYuan(yuan);
        adapter.setXuanzhong(xuanzhong);
        orderList.setAdapter(adapter);
        presenter = new DeliveryOrderPresenter(this);
        String orderId = getIntent().getStringExtra(ORDERID);
        presenter.init(orderId);
        //默认物流配送
        DeliveryOrderActivity.this.presenter.setDistributionType(2);
    }

    public void initView(){
        View headerView = LayoutInflater.from(this).inflate(R.layout.header_order_details_layout, null);
        orderList.addHeaderView(headerView);
        headerViewHolder = new HeaderViewHolder(headerView);
    }

    @Override
    public void showOrderDetails(OrderPosListInfo orderPosListInfo){
        listItems.clear();
        listItems.addAll(orderPosListInfo.getStockPOList());
        number = Integer.parseInt(orderPosListInfo.getNumber());
        LoadImageUtils.loadImage(headerViewHolder.icon, orderPosListInfo.getFile());
        headerViewHolder.title.setText(orderPosListInfo.getPackageName());
        headerViewHolder.quantity.setText(orderPosListInfo.getNumber()+"台");
        headerViewHolder.unitTypeContext.setText(orderPosListInfo.getBrandName());
        headerViewHolder.price.setText(orderPosListInfo.getTotalAmt());
        submitContainer.setVisibility(View.VISIBLE);
    }

    @Override
    public void navSucceed(){
        SuccessfulDeliveryActivity.start(this);
    }

    @Override
    public void navigationLeft(){
        finish();
    }

    @Override
    public void navigationRight(){
    }

    @OnClick(R.id.submit)
    public void onViewClicked(View view){
        switch(view.getId()){
            case R.id.submit:
                MsgChoicePopView choicePopView = new MsgChoicePopView(this,"请确定是否发货?");
                choicePopView.setOnClickListener(this);
                choicePopView.show();
                break;
        }
    }

    @Override
    public void onClickConfirm(View v){
        if(adapter.getNumber() != number){
            showToast("请选择"+number+"台机器");
            return;
        }

        presenter.confirmOut(listItems,number);
    }

    class HeaderViewHolder{
        @BindView(R.id.icon)
        ImageView icon;
        @BindView(R.id.price)
        TextView price;
        @BindView(R.id.quantity)
        TextView quantity;
        @BindView(R.id.type)
        LinearLayout type;
        @BindView(R.id.courier_no)
        EditText courierNo;
        @BindView(R.id.express)
        LinearLayout express;
        @BindView(R.id.unit_type_context)
        TextView unitTypeContext;
        @BindView(R.id.title)
        TextView title;

        public HeaderViewHolder(View headerRootView){
            ButterKnife.bind(this, headerRootView);

            courierNo.addTextChangedListener(new TextWatcher(){
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2){
                    DeliveryOrderActivity.this.presenter.setTrackingNumber(charSequence.toString());
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2){

                }

                @Override
                public void afterTextChanged(Editable editable){
                }
            });
        }


        @OnClick({R.id.express_type,R.id.direct_type})
        public void onViewClicked(View view){
            switch(view.getId()){
                case R.id.express_type://物流配送
                    DeliveryOrderActivity.this.presenter.setDistributionType(2);
                    express.setVisibility(View.VISIBLE);
                    break;
                case R.id.direct_type://当面配送
                    DeliveryOrderActivity.this.presenter.setDistributionType(1);
                    express.setVisibility(View.GONE);
                    break;
            }
        }
    }
}
